package com.yingxin.yxerp.module.report.task.common.job.process;

import cn.hutool.core.util.ObjectUtil;
import com.yingxin.yx.framework.commons.enums.task.DynamicTaskLogCategoryEnum;
import com.yingxin.yx.framework.commons.enums.task.DynamicTaskName;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.helper.DateHelper;
import com.yingxin.yx.framework.commons.vo.MessageInformation;
import com.yingxin.yxerp.module.report.entity.DynamicTaskLog;
import com.yingxin.yxerp.module.report.entity.enums.DynamicTaskLogStatusEnums;
import com.yingxin.yxerp.module.report.service.ITaskExecuteErrorDailyService;
import com.yingxin.yxerp.module.report.task.common.job.handle.JobHandle;
import com.yingxin.yxerp.module.report.task.service.IDynamicTaskService;
import com.yingxin.yxerp.module.report.task.vo.resp.DynamicTaskRespVo;
import com.yingxin.yxerp.module.sale.unified.service.IZhayingSaleCancelPresaleService;
import org.apache.commons.lang3.StringUtils;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 定时任务公共处理类
 * @author yuebao
 * @date 2023-12-12
 */

@Service
public class JobUniversalHandle {

    @Autowired
    private IZhayingSaleCancelPresaleService zhayingSaleCancelPresaleService;

    @Autowired
    private IDynamicTaskService dynamicTaskService;

    @Autowired
    private JobHandle jobHandle;

    @Autowired
    private ITaskExecuteErrorDailyService dailyService;

    /**
     * 处理
     */
    public void handle(JobExecutionContext context, DynamicTaskName taskName ){
        DynamicTaskRespVo vo = getDynamicTaskRespVo( taskName );

        // 创建任务执行记录前部分
        DynamicTaskLog log = createTaskLog( vo, context );

        // 任务执行消息
        String msg = MessageInformation.SUCCESS_MEG;
        // 任务执行状态
        DynamicTaskLogStatusEnums statusEnums = null;

        // 执行任务
        if (taskName == null) {
            statusEnums = DynamicTaskLogStatusEnums.FAIL;
            msg = MessageInformation.UNKNOWN_DOCUMENT;
        }
        try {
            switch (taskName) {
                case TEST:
                    // 测试任务
                    System.out.println( DateHelper.getCurrentDayyyyyMMddHHmmss() );
                    break;
                case CZPB:
                    // 取消轧硬预售卷
                    msg = zhayingSaleCancelPresaleService.cancelZyPresale();
                    break;
                case CZPBL:
                    // 取消轧硬预售单
                    msg = zhayingSaleCancelPresaleService.cancelZyPresaleBill();
                    break;
                case TEERBB:
                    // 任务执行异常提醒日报表
                    msg = dailyService.execute();
                    break;
                default:
                    msg = MessageInformation.UNKNOWN_DOCUMENT;
                    break;
            }
            if ( StringUtils.isEmpty( msg ) ){
                msg = MessageInformation.SUCCESS_MEG;
            }
            statusEnums = DynamicTaskLogStatusEnums.SUCCESS;
        }catch (Exception e){
            statusEnums = DynamicTaskLogStatusEnums.FAIL;
            msg = e.getMessage();
        }finally {
            // 执行
            jobHandle.processingTime( log, msg, statusEnums.getStatus() );
        }
    }

    /**
     * 获取 执行任务名称类型的 任务
     */
    private DynamicTaskRespVo getDynamicTaskRespVo( DynamicTaskName taskName ){
        DynamicTaskRespVo vo = dynamicTaskService.mapper().getTaskNameVo( taskName.getType() );
        if( ObjectUtil.isEmpty( vo ) ){
            throw new BizException( String.format( " [ %s ] 任务不存在！", taskName.getTypeName() ) );
        }
        return vo;
    }

    /**
     * 获取传参中的任务
     */
    private DynamicTaskRespVo getDynamicTask( JobExecutionContext context ){
        JobDataMap jobDataMap = context.getMergedJobDataMap();
        String taskName = null;
        if( jobDataMap.containsKey( "task_name" ) ){
            taskName = jobDataMap.get( "task_name" ).toString();
        }
        if( StringUtils.isEmpty( taskName ) ){
            throw new BizException("任务不存在！");
        }
        return getDynamicTaskRespVo( DynamicTaskName.getEnum( taskName ) );
    }

    /**
     * 创建任务执行记录，前半部分
     */
    private DynamicTaskLog createTaskLog( DynamicTaskRespVo vo, JobExecutionContext context ){
        JobDataMap jobDataMap = context.getMergedJobDataMap();
        String category = null;
        if( jobDataMap.containsKey( "category" ) ){
            category = jobDataMap.get( "category" ).toString();
        }
        if( StringUtils.isEmpty( category ) ){
            category = DynamicTaskLogCategoryEnum.AUTO.getType();
        }
        if( ObjectUtil.isEmpty( vo ) ){
            throw new BizException("任务不存在！");
        }
        DynamicTaskLog log = jobHandle.handle( vo, category );
        return log;
    }

}
